//
//  LoginViewController.m
//  EasyChat
//
//  Created by dllo on 16/6/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LoginViewController.h"
#import "ChatViewController.h"
#import "ContentViewController.h"
#import "ReginViewController.h"
#import "AppDelegate.h"
#import "MyTabBarController.h"
#import "TextFieldBackground.h"//使两个textfield合在一起有圆角有间隔线


@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic, retain) UITextField *userName;
@property (nonatomic, retain) UITextField *passWord;
@property (nonatomic, retain) UIButton *resgin;
@property (nonatomic, retain) UIButton *login;
@property(nonatomic,retain) TextFieldBackground *background;
@property(nonatomic,retain)UILabel *TitleLabel;
@end

@implementation LoginViewController


//环信思想
//1.所有网络请求使用[[EaseMob sharedInstance].chatManager 聊天管理器
//2.





- (void)dealloc
{
    [_userName release];
    [_passWord release];
    [_resgin release];
    [_login release];
    [_TitleLabel release];
    [_background release];
  //   [[EaseMob sharedInstance].chatManager removeDelegate:self];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setHidesBackButton:YES animated:NO];
    [self.navigationController setNavigationBarHidden:YES];
      [self.view setBackgroundColor:[UIColor colorWithRed:51/255.0 green:204/255.0 blue:255/255.0 alpha:1]];
    //标题label
    self.TitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, self.view.frame.size.width, 50)];
    self.TitleLabel.backgroundColor = [UIColor clearColor];
    self.TitleLabel.text = @"EasyChat";
    self.TitleLabel.textColor = [UIColor whiteColor];
    self.TitleLabel.font = [UIFont systemFontOfSize:30];
    self.TitleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.TitleLabel];
    
    
    //封装的view
    _background=[[TextFieldBackground alloc] initWithFrame:CGRectMake(20, 160, self.view.frame.size.width-40, 100)];
    [_background setBackgroundColor:[UIColor whiteColor]];
    [[_background layer] setCornerRadius:5];
    [[_background layer] setMasksToBounds:YES];
    [self.background drawRect:CGRectMake(40, 210, self.view.frame.size.width - 80, 1)];
    [self.view addSubview:_background];

    
    // 创建用户UITextField
    self.userName = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-40, 50)];
    self.userName.backgroundColor = [UIColor clearColor];
    self.userName.layer.cornerRadius= 10;

    self.userName.placeholder=[NSString stringWithFormat:@"请输入您的账号"];
    [self.background addSubview:self.userName];
      self.userName.delegate = self;
    self.userName.delegate = self;
    // 创建密码UITextField
    self.passWord = [[UITextField alloc] initWithFrame:CGRectMake(10, 51, self.view.frame.size.width - 40, 49)];
    self.passWord.backgroundColor = [UIColor clearColor];
        self.passWord.layer.cornerRadius= 10;
    self.passWord.placeholder=[NSString stringWithFormat:@"请输入您的密码"];
    [self.background addSubview:self.passWord];
    
    self.passWord.delegate = self;
    self.passWord.delegate = self;
    [self.passWord addTarget:self  action:@selector(valueChanged)  forControlEvents:UIControlEventEditingChanged];
   // [self.view addSubview:self.passWord];
    
    // 注册按钮
    self.resgin = [UIButton buttonWithType:UIButtonTypeCustom];
    self.resgin.frame = CGRectMake(self.view.frame.size.width - 100,350,80,30);
    [self.resgin addTarget:self action:@selector(resginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.resgin setTitle:@"马上注册" forState:UIControlStateNormal];
    [self.resgin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.resgin.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:self.resgin];
    // 登录按钮
    self.login = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.login setFrame:CGRectMake(20, 270, self.view.frame.size.width-40, 50)];
    
    [self.login setTitle:@"马上登录" forState:UIControlStateNormal];
    [self.login setBackgroundColor:[UIColor colorWithRed:51/255.0 green:102/255.0 blue:255/255.0 alpha:1]];
    self.login.layer.cornerRadius = 10;
    self.login.titleLabel.font = [UIFont systemFontOfSize:22];
    [self.login setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.login addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.login];
    
  //通知  用来改变按钮颜色
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:UITextFieldTextDidChangeNotification object:nil];
    
    
    
}

-(void)changeColor:(id)sender{

    [self.login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    

}


//监听密码textfield  当输入内容后 登录按钮字体颜色变成白色
-(void)valueChanged{

    [self.login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    

}


// 登录按钮的点击方法
- (void)loginAction:(UIButton *)button
{
    //让环信的sdk 在登陆完成之后自动从服务器获取好友列表 添加到本地数据库
    [[EaseMob sharedInstance].chatManager setAutoFetchBuddyList:YES];;

    
    NSString *username = self.userName.text;
    NSString *password = self.passWord.text;
    if (username.length == 0 || password.length == 0) {
        NSLog(@"请输入账号和密码");
        return;
    }
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:username password:password completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error) {
            NSLog(@"登录成功 %@", loginInfo);
                       // 设置自动登录
            [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
            

            MyTabBarController *tabBar = [MyTabBarController new];
            [self.userName resignFirstResponder];
            [self.passWord resignFirstResponder];
            self.view.window.rootViewController = tabBar;
            
            
            
        }else
        {
            NSLog(@"登录失败 %@", error);
        }
        
    } onQueue:dispatch_get_main_queue()];
}

// 注册按钮的点击方法
- (void)resginAction:(UIButton *)button
{
    ReginViewController *registerVC = [ReginViewController new];
//    registerVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"registerVC"];
//    [self showViewController:registerVC sender:nil];
    
    __block typeof(self) pSelf = self;
    registerVC.passValue = ^(NSString *userName, NSString *passWord)
    {
        pSelf.userName.text = userName;
        pSelf.passWord.text = passWord;
    };
    [self presentViewController:registerVC animated:YES completion:^{
    
        
    }];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.userName resignFirstResponder];
    [self.passWord resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.userName resignFirstResponder];
    [self.passWord resignFirstResponder];
    return YES;
    
}



@end
